// 引入express
const express = require('express')
// 引入连接池模块
const pool = require('../pool.js')
const router = express.Router()

// 添加房型
// http://localhost:3000/fx/

router.post('/', (req, res, next) => {
  var obj = req.body
  pool.query('insert into qnls_hotellist set ?', [obj], (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '房源添加成功' })
  })
})
// 修改房型
// http://localhost:3000/fx/xgfx
router.put('/xgfx', (req, res, next) => {
  var obj = req.body
  pool.query(
    'update qnls_hotellist set ? where hid=?',
    [obj, obj.hid],
    (err, r) => {
      if (err) {
        return next(err)
      }
      if (r.changedRows === 0) {
        res.send({ code: 400, msg: '修改失败' })
      } else {
        res.send({ code: 200, msg: '修改成功' })
      }
    }
  )
})
// 删除房型
// http://localhost:3000/fx/1
router.delete('/:hid', (req, res, next) => {
  // 获取params传递参数
  var obj = req.params
  pool.query('delete from qnls_hotellist where hid=?', [obj.hid], (err, r) => {
    if (err) {
      return next(err)
    }
    if (r.affectedRows === 0) {
      res.send({ code: 400, msg: '删除失败' })
    } else {
      res.send({ code: 200, msg: '删除成功' })
    }
  })
})
// 模糊查询
router.post('/name', (req, res, next) => {
  let obj = req.body
  let sql = 'select * from qnls_hotellist where hname like ?'
  pool.query(sql, [`%${obj.hname}%`], (err, r) => {
    if (err) {
      res.send({ code: 500, msg: '查询失败' })
      throw err
    }
    // 将结果封装，返回给客户端
    console.log(r)
    res.send({ code: 200, msg: '查询成功', data: r })
  })
})

// 分页查询
// http://localhost:3000/fx/list
router.get('/list', (req, res, next) => {
  // 获取get传递的参数
  var obj = req.query
  console.log(obj)
  // 判断各项是否为空
  if (!obj.page) {
    obj.pno = 1
  }
  if (!obj.pagesize) {
    obj.count = 5
  }

  // 计算开始查询的值
  var start = (obj.page - 1) * obj.pagesize
  // 将每页数据量转为数字
  var size = parseInt(obj.pagesize)
  // 分页查询 ; 查询总数据量
  pool.query(
    'select * from qnls_hotellist limit ?,?;select count(hid) as sum from qnls_hotellist',
    [start, size],
    (err, r) => {
      if (err) {
        return next(err)
      }
      // 结果是二维数组
      console.log(r)
      // 获取总的数据量
      var t = r[1][0].sum
      // 计算总页数=总数据量/每页显示数据，向上取整
      var p = Math.ceil(t / obj.pagesize)
      // 响应
      res.send({
        code: 200,
        msg: '酒店列表',
        data: r[0],
        total: t,
        pages: p,
        page: obj.page,
        pagesize: obj.pagesize,
        cat_id: obj.cat_id,
      })
    }
  )
})

// 分页模糊查询
// http://localhost:3000/fx/mohu
router.post('/mohu',(req,res,next)=>{
	var obj = req.body
	console.log(obj)
	if(!obj.page){
		obj.page=1
	}
	if(!obj.pagesize){
		obj.pagesize=5
	}
	// 计算开始查询的值
	var start=(obj.page-1)*obj.pagesize
	// 将每页数据量转为数字
	var size=parseInt(obj.pagesize)
	// 分页查询 ; 查询总数据量 

    var c = `%${obj.hname}%`
    console.log(c,'123')
    pool.query('select * from qnls_hotellist where hname like ? limit ?,?;select count(hid) sum from qnls_hotellist where hname like ?',[c,start,size,c],(err,r)=>{
      if(err){
        return next(err)
      }
      console.log(r)
      var t=r[1][0].sum
      if(r.length === 0){
        res.send({code:400,msg:'查询失败'})
      } else {
        res.send({
          code:200,
          msg:'查询成功',
          data:r[0],
          total:t,
          page: obj.page,
			    pagesize: obj.pagesize,
        })
      }
    })
})



module.exports = router
